import axios from 'axios'
import store from '@/store'
import storage from '@/utils/storage'
import notification from 'ant-design-vue/es/notification'
import { Message } from 'ant-design-vue'
import { VueAxios } from './axios'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import { each } from 'lodash'
import moment from 'moment'
// import router from '@/router'

// 创建 axios 实例
const request = axios.create({
  // API 请求的默认前缀
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 20000 // 请求超时时间
})

// 异常拦截处理器
const errorHandler = (error) => {
  error.data = error.data || error.response
  if (error.data) {
    const data = error.data
    // 从 localstorage 获取 token
    const token = storage.get(ACCESS_TOKEN)
    if (error.status === 401 || data.status === 401) {
      notification.error({
        message: '提示',
        description: '登录信息失效，请重新登录！'
      })
      if (token) {
        store.dispatch('Logout').then(() => {
          setTimeout(() => {
            window.location.reload()
          }, 1500)
        })
      } else {
        setTimeout(() => {
          window.location.reload()
        }, 1500)
      }
    } else if (error.data.status === 403) {
      notification.error({
        message: '提示',
        description: '没有操作权限！'
      })
    }else if (error.data.code === 400) {
      notification.error({
        message: '提示',
        description: error.data.msg
      })
    } else if (error.data.code === 401) {
      notification.error({
        message: '提示',
        description: error.data.msg
      })
      window.location.href = '/user/login'
    } else if (error.data.code != 0) {
      notification.error({
        message: '提示',
        description: error.data.msg
      })
    } else {
      return Promise.reject(error.data)
      Message.error(data.msg || '请求出现错误，请重试')
    }
  } else {
    const token = storage.get(ACCESS_TOKEN)
    if (token) {
      store.dispatch('Logout').then(() => {
        setTimeout(() => {
          window.location.reload()
        }, 1500)
      })
    } else {
      setTimeout(() => {
        window.location.reload()
      }, 1500)
    }
    Message.error('请求出现错误，请重试')
  }
  return Promise.reject(error.data)
}

const formatTime = data => {
  each(data, (value, key) => {
    if (moment.isMoment(value)) data[key] = value.format()
  })
}

// request interceptor
request.interceptors.request.use(config => {
  const token = storage.get(ACCESS_TOKEN)
  // 如果 token 存在
  // 让每个请求携带自定义 token 请根据实际情况自行修改
  if (token) {
    config.headers['Authorization'] = token
    // config.headers['Accept'] = 'application/json'
    //config.headers['Authorization'] = 'test1'
  }
  formatTime(config.data)
  formatTime(config.params)
  return config
}, errorHandler)

// response interceptor
request.interceptors.response.use((response) => {
  const data = response.data
  if (data.code!==0) return errorHandler(response)
  return data.data
}, errorHandler)

const installer = {
  vm: {},
  install(Vue) {
    Vue.use(VueAxios, request)
  }
}

export default request

export {
  installer as VueAxios,
  request as axios
}
